package com.example.administrator.youlu.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.TypedValue;

public class ImageManager {

    /**
     * @param mContext
     * @param iconBitmap
     * @return
     * 格式化头像
     */
    public static Bitmap formatBitmap(Context mContext,Bitmap iconBitmap){

        //获取原始头像宽高
        int height=iconBitmap.getHeight();
        int width=iconBitmap.getWidth();

        // 创建一个画布背景图片
        Bitmap backBitmap= Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
        //创建一个画布
        Canvas canvas =new Canvas(backBitmap);

        //创建一个画笔
        Paint paint=new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);

        int radius = Math.min(height,width)/2;
        canvas.drawCircle(width/2,height/2,radius,paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(iconBitmap,0,0,paint);

        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        //dp转px
        float ws = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,2,mContext.getResources().getDisplayMetrics());
        paint.setStrokeWidth(ws);

        canvas.drawCircle(width/2,height/2,radius,paint);

        return backBitmap;
    }
}
